﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace DupeFilter
{
    class DirectoryHelper
    {
        //Adapted from http://dotnetperls.com/Content/Recursively-Find-Files.aspx
        public static List<string> GetAllDirectories(string initialDirectory)
        {
            List<string> result = new List<string>(); // Store results in the directory results list.

            Stack<string> directoryStack = new Stack<string>(); // Store a stack of our directories.

            directoryStack.Push(initialDirectory); // Add initial directory.

            while (directoryStack.Count > 0) // Continue while there are directories to process
            {
                string currentDirectory = directoryStack.Pop(); // Get top directory
                result.Add(currentDirectory);

                try
                {
                    foreach (string subDirectory in (Directory.GetDirectories(currentDirectory).Reverse())) // Add all directories at this directory.
                    {
                        directoryStack.Push(subDirectory);
                    }
                }
                catch
                {
                    // Could not open the directory
                }
            }

            return result;
        }

        //Adapted from http://dotnetperls.com/Content/Recursively-Find-Files.aspx
        public static List<string> GetAllFiles(string initialDirectory)
        {
            List<string> result = new List<string>(); // Store results in the directory results list.

            Stack<string> directoryStack = new Stack<string>(); // Store a stack of our directories.

            directoryStack.Push(initialDirectory); // Add initial directory.

            while (directoryStack.Count > 0) // Continue while there are directories to process
            {
                string currentDirectory = directoryStack.Pop(); // Get top directory

                try
                {
                    result.AddRange(Directory.GetFiles(currentDirectory, "*.*")); // Add all files at this directory to the result List.
                    foreach (string subDirectory in (Directory.GetDirectories(currentDirectory).Reverse())) // Add all directories at this directory.
                    {
                        directoryStack.Push(subDirectory);
                    }
                }
                catch
                {
                    // Could not open the directory
                }
            }

            return result;
        }
    }
}
